source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

platform :ios, '13.0'

target 'XResources_Example' do
  pod 'XResources', :path => '../'
#  pod 'R.swift', '~> 7.0'  # https://github.com/mac-cain13/R.swift
  
  target 'XResources_Tests' do
    inherit! :search_paths

    pod 'Quick', '~> 6.0'  # https://github.com/Quick/Quick
    pod 'Nimble', '~> 11.0'  # https://github.com/Quick/Nimble
#    pod 'RxAtomic', :modular_headers => true
    pod 'RxBlocking'  # https://github.com/ReactiveX/RxSwift
  end
end

post_install do |installer|
    # Cocoapods optimization, always clean project after pod updating
    Dir.glob(installer.sandbox.target_support_files_root + "Pods-*/*.sh").each do |script|
        flag_name = File.basename(script, ".sh") + "-Installation-Flag"
        folder = "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
        file = File.join(folder, flag_name)
        content = File.read(script)
        content.gsub!(/set -e/, "set -e\nKG_FILE=\"#{file}\"\nif [ -f \"$KG_FILE\" ]; then exit 0; fi\nmkdir -p \"#{folder}\"\ntouch \"$KG_FILE\"")
        File.write(script, content)
    end
    
    # Enable tracing resources
    installer.pods_project.targets.each do |target|
      if target.name == 'RxSwift'
        target.build_configurations.each do |config|
          if config.name == 'Debug'
            config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['-D', 'TRACE_RESOURCES']
          end
        end
      end
    end
    
    # To hide deployment target warnings
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'
      end
    end
    
    installer.pods_project.targets.each do |target|
      # 添加 R.swift 生成的脚本
      if target.name == 'XResources'
        # 检查是否已经存在 R.swift 脚本
        existing_phase = target.build_phases.find do |phase|
          phase.respond_to?(:name) && phase.name == 'R.swift'
        end

        unless existing_phase
          # 添加自定义脚本到 Build Phases
          phase = target.new_shell_script_build_phase('R.swift')
          phase.shell_script = <<-SCRIPT
            #!/bin/sh
            # 自定义脚本逻辑
            "$PODS_ROOT/R.swift/rswift" generate --access-level public "$SRCROOT/../../XResources/Classes/R.generated.swift"
          SCRIPT

          # 设置输出文件路径
          phase.output_paths = ["$SRCROOT/../../XResources/Classes/R.generated.swift"]

#          # 获取所有构建阶段
#          build_phases = target.build_phases
#
#          # 查找 `Compile Sources` 构建阶段
#          compile_sources_phase = build_phases.find do |phase|
#            phase.respond_to?(:name) && phase.name == 'Compile Sources'
#          end

#          if compile_sources_phase
#            # 将自定义脚本移动到 `Compile Sources` 之前
#            target.build_phases.insert(build_phases.index(compile_sources_phase), phase)
#          else
#            # 如果找不到 `Compile Sources`，将自定义脚本添加到最后
#            target.build_phases << phase
#          end
        end
      end
    end
end
